package com.ghpt.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ghpt.dto.GongyeDailyAvgDTO;
import com.ghpt.mapper.GongYeXQMapper;
import com.ghpt.pojo.GongYeXQ;
import com.ghpt.service.GongYeXQService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class GongYeXQServiceImpl implements GongYeXQService {
    @Autowired
    private GongYeXQMapper gongYeXQMapper;

    @Override
    public GongYeXQ findGongYeXQById(Long id) {
        if (id != null) {
            return gongYeXQMapper.selectById(id);
        }else {
            return null;
        }
    }
    @Override
    public int updateById(GongYeXQ gongYeXQ) {
        return gongYeXQMapper.updateById(gongYeXQ);//更新成功为1否则为0
    }
    @Override
    public int updateValue(Long id, Double value) {
        UpdateWrapper<GongYeXQ> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id).set("value", value);
        return gongYeXQMapper.update(null, updateWrapper);
    }

    @Override
    public IPage<GongYeXQ> findGongYeXQByPage(Page<GongYeXQ> pageParam) {
        // 查询id从97开始的数据
        LambdaQueryWrapper<GongYeXQ> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ge(GongYeXQ::getId, 97);
        return gongYeXQMapper.selectPage(pageParam, queryWrapper);
    }

    @Override
    public List<GongyeDailyAvgDTO> calculateDailyAverage(int years) {
        List<GongyeDailyAvgDTO> dailyAverages = new ArrayList<>();
        int daysPerYear = 365; // 每年365天
        int hoursPerDay = 24;  // 每天24小时
        int recordsPerYear = daysPerYear * hoursPerDay; // 每年记录数

        // 计算总需要处理的天数
        int totalDays = daysPerYear * years;

        for (int day = 1; day <= totalDays; day++) {
            // 计算当前天对应的年和年内天数
            int currentYear = (day - 1) / daysPerYear + 1;
            int dayInYear = (day - 1) % daysPerYear + 1;

            // 计算当前天的起始和结束ID
            // 假设基础ID从97开始，每年数据连续存储
            long startId = 97 + (long) (currentYear - 1) * recordsPerYear + (dayInYear - 1) * hoursPerDay;
            long endId = startId + hoursPerDay - 1;

            // 查询当天的24条数据
            LambdaQueryWrapper<GongYeXQ> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.between(GongYeXQ::getId, startId, endId);
            List<GongYeXQ> dailyData = gongYeXQMapper.selectList(queryWrapper);

            // 计算平均值
            double sum = 0;
            for (GongYeXQ data : dailyData) {
                sum += data.getValue();
            }
            double average = !dailyData.isEmpty() ? sum / dailyData.size() : 0;

            // 构建结果对象，注意这里dayOfYear在多年情况下应该是连续的
            int displayDay = (currentYear - 1) * daysPerYear + dayInYear;
            dailyAverages.add(new GongyeDailyAvgDTO(displayDay, average));
        }
        System.out.println(years);
        return dailyAverages;
    }

    @Override
    public void replaceRows(List<GongYeXQ> data) {

    }

    @Override
    public List<String> getTableColumns() {
        return Arrays.asList("id", "value");
    }

    @Override
    public void replaceRows(Long startId, Long endId, List<GongYeXQ> data) {
        QueryWrapper<GongYeXQ> wrapper = new QueryWrapper<>();
        wrapper.between("id", startId, endId);
        gongYeXQMapper.delete(wrapper);

        for (GongYeXQ e : data) {
            gongYeXQMapper.insert(e);
        }
    }
}
